package com.scanner.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.scanner.hardware.MyApplication;
import com.scanner.hardware.R;

/**
 * 公共提示弹框
 */
public class PublicTipsDialog extends DialogFragment {

    private Button btnSure;
    private Button btnCancel;
    private OnDialogBtnClickListener onDialogBtnClickListener = null;
    private String cancelText;
    private String sureText;

    public static PublicTipsDialog getInstance(String tips){
        PublicTipsDialog tipsDialog = new PublicTipsDialog();
        Bundle bundle = new Bundle();
        bundle.putString("tips",tips);
        tipsDialog.setArguments(bundle);
        return tipsDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(android.app.DialogFragment.STYLE_NO_TITLE, R.style.dialog_public_style);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_public_tips_layout,null);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout((int) (0.8* MyApplication.getPhoneWidthMetrics()),ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String tips = getArguments().getString("tips");
        TextView tvTips = view.findViewById(R.id.tv_dialog_tips);
        tvTips.setText(TextUtils.isEmpty(tips) ? view.getContext().getString(R.string.tips_nono) : tips);
        btnSure = view.findViewById(R.id.btn_update);
        btnCancel = view.findViewById(R.id.btn_cancel);

        btnCancel.setText(TextUtils.isEmpty(cancelText) ? getString(R.string.dis_data) : cancelText);
        btnSure.setText(TextUtils.isEmpty(sureText) ? getString(R.string.all_data) : sureText);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onDialogBtnClickListener != null){
                    onDialogBtnClickListener.sureOnClick();
                }
                dissDialog();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onDialogBtnClickListener != null){
                    onDialogBtnClickListener.cancelOnClick();
                }
                dissDialog();

            }
        });
    }


    public interface OnDialogBtnClickListener{
        void sureOnClick();
        void cancelOnClick();
    }

    public PublicTipsDialog setCancelText(String cancelText){
        this.cancelText = cancelText;
        return this;
    }

    public PublicTipsDialog setSureText(String sureText){
        this.sureText = sureText;
        return this;
    }

    public void addDialogOnClick(OnDialogBtnClickListener listener){
        onDialogBtnClickListener = listener;
    }

    public void dissDialog(){
        if(getDialog()!= null && getDialog().isShowing()){
            getDialog().dismiss();
        }
    }
}
